/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view;

/**
 *
 * @author Laurent
 */
public class CollapsePanel extends javax.swing.JPanel {
    public static final int minWidth = 150;
    private static final int buttonSize = 20;
    
    private javax.swing.JButton button;
    private javax.swing.JComponent content;
    
    public CollapsePanel(String title) {
        javax.swing.JLabel label = new javax.swing.JLabel(title);
        label.setHorizontalAlignment(javax.swing.JLabel.CENTER);
        label.setVerticalAlignment(javax.swing.JLabel.CENTER);
        label.setMaximumSize(new java.awt.Dimension(Integer.MAX_VALUE, 20));
        label.setPreferredSize(new java.awt.Dimension(minWidth - buttonSize, 20));
        label.setMinimumSize(new java.awt.Dimension(minWidth - buttonSize, 20));
        label.setAlignmentY(java.awt.Component.CENTER_ALIGNMENT);
        
        button = new javax.swing.JButton(IconProvider.getEmbedded("toggle.png"));
        button.setMaximumSize(new java.awt.Dimension(buttonSize, buttonSize));
        button.setPreferredSize(new java.awt.Dimension(buttonSize, buttonSize));
        button.setMinimumSize(new java.awt.Dimension(buttonSize, buttonSize));
        button.setAlignmentY(java.awt.Component.CENTER_ALIGNMENT);
        
        javax.swing.JPanel header = new javax.swing.JPanel();
        header.setOpaque(true);
        header.setBackground(java.awt.Color.GRAY);
        header.setLayout(new javax.swing.BoxLayout(header, javax.swing.BoxLayout.X_AXIS));
        header.setMaximumSize(new java.awt.Dimension(Integer.MAX_VALUE, buttonSize));
        header.setPreferredSize(new java.awt.Dimension(minWidth, buttonSize));
        header.setMinimumSize(new java.awt.Dimension(minWidth, buttonSize));
        header.add(button);
        header.add(label);
        header.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
        
        this.setOpaque(false);
        this.setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS));
        this.add(header);
        
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override public void mouseReleased(java.awt.event.MouseEvent evt) {
                if (CollapsePanel.this.getComponentCount() == 1) {
                    CollapsePanel.this.add(content);
                    button.setIcon(IconProvider.getEmbedded("toggle.png"));
                    revalidate();
                    repaint();
                } else {
                    CollapsePanel.this.remove(content);
                    button.setIcon(IconProvider.getEmbedded("toggle-expand.png"));
                    revalidate();
                    repaint();
                }
            }
        });
    }
    
    protected void setContent(javax.swing.JComponent panel) {
        if (this.content != null)
            this.remove(this.content);
        this.content = panel;
        this.content.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
        this.add(this.content);
    }
}
